<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @change="changeDate"
      @confirm="loadBirthday"
    />
  </div>
</template>

<script>
import { updateProfileAPI } from "@/api";
import dayjs from 'dayjs'
export default {
  name: "",
  props: {
    birthday: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      minDate: new Date(1990, 11, 20),
      maxDate: new Date(2025, 11, 20),
      currentDate: new Date(this.birthday),
    };
  },
  methods: {
    changeDate() {
      this.localDate = this.currentDate;
    },
    async loadBirthday() {
      try {
        await updateProfileAPI({
          birthday: dayjs(this.currentDate).format("YYYY-MM-DD"),
        });
        this.$emit(
          "update:birthday",
          dayjs(this.currentDate).format("YYYY-MM-DD")
        );
        this.$emit("close");
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style>
</style>